##JOHNTALBOT
## THIS IS A MULTIFUNCTION SCRIPT THAT PLOTS POINTS ALONG TRANSECT LINES
## THESE POINT POSTITIONS ARE BASED FROM REGRESSION RATES FROM EXTERNAL CSVS,
## DISTANCES SET HERE AND BASELINE POINTS ON EACH TRANSECT REPRESENTING
## CURRENT SHORE POSITION.

import arcpy, csv
from arcpy import env
env.overwriteOutput='TRUE'
## Main function:
##In the main function, specify the gdb for workspace and the root output csv name
## variable. 

def main():
    ##list the islands that need to be analyzed. (usually 1-12)
    list=[2,3,4,5,6,7,8,9,10,11,12,13,14]
    global period, island_num, csv_name, years
    csv_name="i"
    #set years in future to predict
    years=140
    
    
    for i in list:
        island_num=str(i)
        print "Working...."
        print
        print
        print
        print i
        print
        print
        #call func mainy (renamed from main for iteration)
        mainy(i)
    
def mainy(num):

    ##Pathways for transects = fc. Pathways for current shoreline intersections = fp
    fc="E:\\cp\\mapData\\geo.gdb\\Trans"+str(island_num)+"UTM"
    fp="E:\\cp\\mapData\\geo.gdb\\Intersect"+str(island_num)
    ##Fields of interest include shape@ geometery and uniq5ue transect id
    fields=["SHAPE@","OBJECTID","TransOrder"]
    ##Create two scursors for iteration through both feature classes
    scursor=arcpy.da.SearchCursor(fc,fields)
    pcursor=arcpy.da.SearchCursor(fp,fields)

    

    glist=[]
    ## For the below itteration:
    ## we will obtain the geometery of the transect
    ## call function findIt to search corresponding CSV for rate of regression
    ## 
    for i in scursor:
        
        geom=i[0]
        reg=findIt(i[2])
        
        if reg== None:
            print "No Match for", i[2]
            continue
        for p in pcursor:
            if p[2]==i[2]:
                intp=p[0]
                break
            intp=0
        pcursor.reset()
        if intp != 0:
            startdist= intp.distanceTo(geom.firstPoint)
            if reg != -9999999999:
                pos=move(reg,geom,startdist)
                glist.append(pos)
    
    
    arcpy.CopyFeatures_management(glist, "E:\\cp\\mapData\\a7a"+str(island_num)+"_"+str(2010+years))



def findIt(tid):
    
    with open("E:\\cp\\mapData\\"+str(csv_name+island_num)+".csv",'rb') as csvf:
        cread = csv.reader(csvf)
    
        for i in cread:
            
            if str(i[0])== str(tid):
               
                num=i[3]
                if num=="NULL":
                    #num=0
                    num= -9999999999               
                return float(num)
def move(reg,geom,start):
        length=geom.length
        dist = start+(reg*years)
        pos=geom.positionAlongLine(dist)
        return pos

    
    
main()  
